﻿#pragma once

#include <iostream>
using namespace std;

namespace TL
{
	enum color
	{
		RED,
		BLACK
	};

	template<class K, class V>
	struct RBTreeNode
	{
		pair<K, V> _kv;
		RBTreeNode<K, V>* _left;
		RBTreeNode<K, V>* _right;
		RBTreeNode<K, V>* _parent;
		color _col;

		RBTreeNode(const pair<K,V>& kv)
			:_kv(kv)
			,_left(nullptr)
			,_right(nullptr)
			,_parent(nullptr)
		{}
	};

	template<class K, class V>
	class RBTree
	{
		typedef RBTreeNode<K, V> Node;
		void RotateR(Node* parent)
		{
			Node* ppNode = parent->_parent;
			Node* subL = parent->_left;
			Node* subLR = subL->_right;

			//调整指针指向
			parent->_left = subLR;
			subL->_right = parent;

			//根据判断看看是否要对根或ppNode进行修改
			if (parent == _root)
			{
				_root = subL;
			}
			else
			{
				if (parent == ppNode->_left)
					ppNode->_left = subL;
				else
					ppNode->_right = subL;
			}
			
			//调整父指针指向
			parent->_parent = subL;
			subL->_parent = ppNode;
			if (subLR)
				subLR->_parent = parent;
		}

		void RotateL(Node* parent)
		{
			Node* ppNode = parent->_parent;
			Node* subR = parent->_right;
			Node* subRL = subR->_left;

			//修改指针指向
			parent->_right = subR->_left;
			subR->_left = parent;
			
			//根据判断看看是否要对根或ppNode进行修改
			if (parent == _root)
			{
				_root = subR;
			}
			else
			{
				if (parent == ppNode->_left)
					ppNode->_left = subR;
				else
					ppNode->_right = subR;
			}

			//修改父指针指向
			parent->_parent = subR;
			subR->_parent = ppNode;
			if (subRL)
				subRL->_parent = parent;
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_kv.first << " : " << root->_kv.second << endl;
			_InOrder(root->_right);
		}

		int _size(Node* root)
		{
			if (root == nullptr)
			{
				return 0;
			}
			return _size(root->_left) + _size(root->_right) + 1;
		}

		int _height(Node* root)
		{
			if (root == nullptr)
			{
				return 0;
			}
			int left = _height(root->_left);
			int right = _height(root->_right);
			return left > right ? left + 1 : right + 1;
		}
		bool _IsBalance(Node* root, int blacknum, int refnum)
		{
			if (root == nullptr)
			{
				if (blacknum != refnum)
				{
					cout << "某条路径黑色节点与最左路径黑色节点个数不同" << endl;
					return false;
				}
				return true;
			}

			if (root->_parent && root->_col == RED && root->_parent->_col == RED)
			{
				cout << root->_kv.first << "存在连续的红色结点" << endl;
				return false;
			}

			if (root->_col == BLACK)
			{
				blacknum++;
			}
			
			return _IsBalance(root->_left, blacknum, refnum)
				&& _IsBalance(root->_right, blacknum, refnum);
		}

	public:
		bool IsBalance()
		{
			if (_root->_col != BLACK)
			{
				cout << "根节点不为黑色" << endl;
				return false;
			}
			
			//选最左边作为参考值
			Node* cur = _root;
			int refnum = 0;
			while (cur)
			{
				if (cur->_col == BLACK)
				{
					refnum++;
				}
				cur = cur->_left;
			}
			return _IsBalance(_root, 0, refnum);
		}

		void InOrder()
		{
			_InOrder(_root);
		}

		int size()
		{
			return _size(_root);
		}

		int height()
		{
			return _height(_root);
		}
		
		Node* find(const K& x)
		{
			Node* cur = _root;
			while (cur)
			{
				if (x > cur->_kv.first)
				{
					cur = cur->_right;
				}
				else if(x < cur->_kv.first)
				{
					cur = cur->_left;
				}
				else
				{
					return cur;
				}
			}
			return nullptr;
		}

		bool insert(const pair<K, V>& kv)
		{
			if (_root == nullptr)
			{
				_root = new Node(kv);
				_root->_col = BLACK;
			}
			else
			{
				Node* cur = _root;
				Node* parent = _root;
				while (cur)
				{
					if (kv.first > cur->_kv.first)
					{
						parent = cur;
						cur = cur->_right;
					}
					else if (kv.first < cur->_kv.first)
					{
						parent = cur;
						cur = cur->_left;
					}
					else
					{
						return false;
					}
				}
				cur = new Node(kv);
				cur->_col = RED;
				cur->_parent = parent;
				if (kv.first > parent->_kv.first)
				{
					parent->_right = cur;
				}
				else
				{
					parent->_left = cur;
				}

				while (parent && parent->_col == RED)
				{
					Node* grandfather = parent->_parent;
					if (parent == grandfather->_left)
					{
						Node* uncle = grandfather->_right;
						if (uncle && uncle->_col == RED)
						{
							parent->_col = uncle->_col = BLACK;
							grandfather->_col = RED;
							cur = grandfather;
							parent = cur->_parent;
						}
						else
						{
							if (cur == parent->_left)
							{
								RotateR(grandfather);
								parent->_col = BLACK;
								grandfather->_col = RED;
							}
							else
							{
								RotateL(parent);
								RotateR(grandfather);
								cur->_col = BLACK;
								grandfather->_col = RED;
							}
							break;
						}
					}
					else
					{
						Node* uncle = grandfather->_left;
						if (uncle && uncle->_col == RED)
						{
							parent->_col = uncle->_col = BLACK;
							grandfather->_col = RED;
							cur = grandfather;
							parent = cur->_parent;
						}
						else
						{
							if (cur == parent->_right)
							{
								RotateL(grandfather);
								parent->_col = BLACK;
								grandfather->_col = RED;
							}
							else
							{
								RotateR(parent);
								RotateL(grandfather);
								cur->_col = BLACK;
								grandfather->_col = RED;
							}
							break;
						}
					}
				}
			}
			_root->_col = BLACK;
			return true;
		}

	private:
		Node* _root = nullptr;
	};
}